<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<div class="class-index-con-right">
	<div class="row">
		<div class="col-md-12">
			<div class="small-class-list-title">
				小班列表
				<shiro:hasRole name="teacher">
					<input type="button" class="btn btn-primary" id="new-small-class-btn" value="新建小班" style="margin-left:1rem;" >
				</shiro:hasRole>
				
			</div>
			<div class="small-class-list">
				<div class="manage-team">
					<span>行政小班</span>
				</div>
				<div class="task-team">
					<span>个性化小班</span>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="new-small-class">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title" style="display:inline;float:left;">新建小班</h4>
				<button type="button" class="close" data-dismiss="modal" style="display:inline;float:right;">&times;</button>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						<label>小班类型</label>
						<select class="form-control" id="createClassType" onchange="selectClassType()">
							<option value="0" selected="selected">行政小班</option>
							<option value="1">个性化小班</option>
						</select>
					</div>
					<div class="col-xs-12">
						<label>小班名称</label>
						<input type="text" class="form-control" name="smallClassName">
					</div>
					<div class="col-xs-12" id="assi-box">
						<label>选择助教</label>
						<select class="form-control" id="smallClassAssistantList">
							<option></option>
						</select>
					</div>
				</div>
			</div>
			<div class="modal-footer" style="text-align:center;margin-top:2rem;">
				<button type="button" class="btn btn-primary" id="createSmallClassButton">确定</button>
			</div>
		</div>
	</div>
</div>

<script>

$(function(){
	$("#new-small-class-btn").click(function() {
		$("#new-small-class").modal("show");
		initAssistantValue();
	});
	$("#createSmallClassButton").click(createSmallClass);
});
//新建小班
var selectClassType = function() {
	var classType = $("#createClassType option:selected").val();
	console.log(classType);
	if(classType==0) {
		$("#assi-box").show();
		initAssistantValue();
	} else {
		$("#assi-box").hide();
	}
}
var initAssistantValue = function(){
	var courseId=data.data.classView.courseId;
	$.get(CONTEXT_PATH+"accontrol/ajaxListAssistant?id="+courseId,function(data) {
		if(data.status=="success"){
			var assistantList = $("select[id='smallClassAssistantList']");
			assistantList.empty();
    		var s="";
    		var assistants=data.data.Assistants;
    		for(var i=0;i<assistants.length;i++){
    			s+="<option value=\""+assistants[i].id+"\">"+assistants[i].name+"</option>";
    		}
    		assistantList.append(s);
		}else{
			alert(data.message);
		}
	}); 
}
var createSmallClass = function() {
	var courseId = data.data.classView.courseId;
	var classId = data.data.classView.id;
	var classType = $("#createClassType").val();
	var className = $("input[name='smallClassName']").val();
	var smallClassAssistantId=$("#smallClassAssistantList").val();
	var assistantCode = $("#smallClassAssistantList option:selected").text();
	if(classType==0){//行政小班
		if(className == null || className == "") {
			alert("请填写小班名称！");
		}else {
			if(smallClassAssistantId == null){
				alert("请选择一个助教");
			}else{
				var info = "你要创建的是<行政小班>,小班名称为<"+className+">,助教为<"+assistantCode+">";
				alert(info);
				var json={
						name:className,
						courseId:courseId,
						classType:"MANAGETEAM",
						fid:classId,
						managerId:smallClassAssistantId
					}
				$.post(CONTEXT_PATH+"class/ajaxCreate", {
					data : JSON.stringify(json)
				}, function(data) {
					if(data.status=="success"){
		    			window.location.reload();
					}else{
						alert(data.message);
					}
				});
			}
			
		}		
		
	}else if(classType==1) {
		if(className == null || className == "") {
			alert("请填写小班名称！");
		}else {
			var info = "你要创建的是<个性化小班>,小班名称为<"+className+">";
			alert(info);			
			var json={
					name:className,
					classType:"TASKTEAM",
					fid:classId,
					courseId:courseId
				}
			$.post(CONTEXT_PATH+"class/ajaxCreate", {
				data : JSON.stringify(json)
			}, function(data) {
				if(data.status=="success"){
					window.location.reload();
				}else{
					alert(data.message);
				}
			});
		}
		
	} else {
		alert("wrong");
	}
}
// 小班列表
var initClass = function(){
	var fid=data.data.classView.id;
	var json={
		fid:fid
	};
	$.get(CONTEXT_PATH+"class/ajaxListData",{
		data : JSON.stringify(json)
	},function(data) {
		if(data.status=="success"){
			var mlist=$(".manage-team");
			var tlist=$(".task-team");
			var ms="";
			var ts="";
			if(data.data.list.length>0){
				var item=data.data.list;
				ms+=("<ul>");
				ts+=("<ul>");
				for(var i=0;i<item.length;i++){
					if(item[i].classType == "MANAGETEAM") {
						ms+=("<li onclick=getSmallStudentList("+item[i].id+",1)>"+item[i].name+"</li>");
					}
					else if(item[i].classType == "TASKTEAM") {
						ts+=("<li onclick=getSmallStudentList("+item[i].id+",1)>"+item[i].name+"</li>");
					}
				}
				ms+=("</ul>");
				ts+=("</ul>");
			}
			mlist.append(ms);
			tlist.append(ts);
		} else {
			alert(data.message);
		}
	});
}



</script>